@echo off
pushd
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

:get_list_file
set list_file=%~1
if not defined list_file (
    echo Usage: %~nx0 LIST_FILE
    goto :eof
)
if not exist "!list_file!" (
    echo ERROR: !list_file!: not exists
    goto :eof
)

echo Got list file: !list_file!

:get_temp_dir
set temp_dir=!time::=_!
set temp_dir=!temp_dir: =!
if exist \!temp_dir! goto get_temp_dir
echo Temp Dir: !temp_dir!

for /f "delims=" %%i in (!list_file!) do (
    echo [Directory]
    echo %%i

    set temp_path=%%~di\!temp_dir!
    if not exist !temp_path! (
        echo NOT EXISTS: !temp_path!
        mkdir !temp_path!
        if errorlevel 1 (
            echo ERROR: cannot make dir: !temp_path!
            goto :eof
        )
    )
    echo Temporarily moving information files to store...
    move "%%i\*.nfo" !temp_path! 2>nul
    move "%%i\*.jpg" !temp_path! 2>nul
    move "%%i\*.jpeg" !temp_path! 2>nul
    move "%%i\*.png" !temp_path! 2>nul
    move "%%i\*.bmp" !temp_path! 2>nul
    move "%%i\*.gif" !temp_path! 2>nul
    move "%%i\*.txt" !temp_path! 2>nul
    echo Deleting...
    for /d %%d in ("%%i\*COMPLETE*") do rmdir /s /q "%%d" 2>nul
    for /d %%d in ("%%i\Sub*") do rmdir /s /q "%%d" 2>nul
    for /d %%d in ("%%i\CD*") do rmdir /s /q "%%d" 2>nul
    rmdir /s /q "%%i\Sample" 2>nul
    del /s /q /f "%%i\*"
    echo Restoring information files...
    move !temp_path!\* "%%i"
)

rmdir /s /q !temp_path!
